package com.tongtech.system.service.impl;

import com.tongtech.system.api.domain.SysUser;
import com.tongtech.system.domain.WorkOrderSupport;
import com.tongtech.system.mapper.WorkOrderSupportMapper;
import com.tongtech.system.service.IWorkOrderSupportService;
import com.tongtech.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 工单支持人员管理 服务实现
 *
 * @author xingyc
 */
@Service
public class WorkOrderSupportServiceImpl implements IWorkOrderSupportService
{
    @Autowired
    private WorkOrderSupportMapper workOrderSupportMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询工单支持人员管理数据
     *
     * @param workOrderSupport 工单支持人员信息
     * @return 工单支持人员信息集合
     */
    @Override
    public List<WorkOrderSupport> selectWorkOrderSupportList(WorkOrderSupport workOrderSupport)
    {
        List<WorkOrderSupport> list = workOrderSupportMapper.selectWorkOrderSupportList(workOrderSupport);
        if(!CollectionUtils.isEmpty(list)){
            list.stream().forEach(item -> {
               Long supportPerson = item.getSupportPerson();
               if(Objects.nonNull(supportPerson)){
                   SysUser user = userService.selectUserById(supportPerson);
                   if(Objects.nonNull(user)){
                      String nickName = user.getNickName();
                      item.setSupportPersonName(nickName);
                   }
               }
            });
        }
        return list;
    }

    /**
     * 根据工单支持人员ID查询信息
     *
     * @param workOrderSupportId 工单支持人员ID
     * @return 工单支持人员信息
     */
    @Override
    public WorkOrderSupport selectWorkOrderSupportById(Long workOrderSupportId)
    {
        WorkOrderSupport workOrderSupport = workOrderSupportMapper.selectWorkOrderSupportById(workOrderSupportId);
        return workOrderSupport;
    }

    /**
     * 新增保存工单支持人员信息
     *
     * @param workOrderSupport 工单支持人员信息
     * @return 结果
     */
    @Override
    public int insertWorkOrderSupport(WorkOrderSupport workOrderSupport)
    {
        return workOrderSupportMapper.insertWorkOrderSupport(workOrderSupport);
    }

    /**
     * 修改保存工单支持人员信息
     *
     * @param workOrderSupport 工单支持人员信息
     * @return 结果
     */
    @Override
    public int updateWorkOrderSupport(WorkOrderSupport workOrderSupport)
    {
        int result = workOrderSupportMapper.updateWorkOrderSupport(workOrderSupport);
        return result;
    }

    /**
     * 删除工单支持人员管理信息
     *
     * @param deptId 工单支持人员ID
     * @return 结果
     */
    @Override
    public int deleteWorkOrderSupportById(Long deptId)
    {
        return workOrderSupportMapper.deleteWorkOrderSupportById(deptId);
    }

    @Override
    public int deleteWorkOrderSupportByWorkOrderId(Long workOrderId)
    {
        return workOrderSupportMapper.deleteWorkOrderSupportByWorkOrderId(workOrderId);
    }


}
